Questions
20 of 30
1What are functions and operators in MySQL, and how are they different?
2What is the difference between single-row functions and aggregate functions?
3Give examples of commonly used string functions in MySQL.
4What is the use of the CONCAT() function? How is it different from using ||?
5Explain the difference between NOW(), CURDATE(), and SYSDATE().
6What are arithmetic operators in MySQL? Give examples.
7What is the difference between the = and <=> operators?
8What does the DISTINCT keyword do when used with aggregate functions like COUNT()?
9What is the difference between IFNULL() and COALESCE() functions?
10What are logical operators in MySQL, and how do AND, OR, and NOT work?
11What is the difference between LENGTH() and CHAR_LENGTH() functions?
12How does MySQL handle type conversion when using operators on different data types?
13What is the difference between ROUND(), TRUNCATE(), and FORMAT() functions?
14Explain how CASE and IF() functions can be used for conditional logic.
15What are comparison operators in MySQL, and how are they used with NULL values?
16How do aggregate functions like AVG(), SUM(), and MAX() behave when NULLs are present?
17Explain the use of REGEXP and LIKE operators. What’s the difference between them?
18What are user-defined functions (UDFs), and how do they differ from stored procedures?
19What are the differences between DATE_ADD() and ADDDATE() functions?
20How can you use STR_TO_DATE() and DATE_FORMAT() for converting and displaying date values?
21How does MySQL internally optimize and cache results of deterministic functions?
22What are window functions in MySQL 8.0, and how do they differ from aggregate functions?
23Explain the difference between RANK(), DENSE_RANK(), and ROW_NUMBER() window functions.
24How does MySQL evaluate operator precedence when multiple operators are used in a single expression?
25Can functions be used in the WHERE clause? What are the performance implications?
26How can you use JSON_EXTRACT() and JSON_CONTAINS() to work with JSON data in MySQL?
27What are the performance trade-offs of using scalar functions inside JOIN or GROUP BY clauses?
28Explain how collation affects comparison operators in string functions.
29What are deterministic and non-deterministic functions? How does this affect replication and indexes?
30How would you combine multiple functions and operators to clean, transform, and aggregate data efficiently in one query?
20 / 30

How can you use STR_TO_DATE() and DATE_FORMAT() for converting and displaying date values?

Using STR_TO_DATE() and DATE_FORMAT() in MySQL

MySQL provides two important functions for working with date strings: STR_TO_DATE() for converting strings into date values, and DATE_FORMAT() for converting date values into formatted strings.

1. STR_TO_DATE(): Convert String → Date
  1. 1

    Parses a string based on a specified format and returns a DATE, DATETIME, or TIME value.

  2. 2

    Useful when importing data where dates are stored as strings.

  3. 3

    Format specifiers (e.g., %d, %m, %Y) must match the input string.

STR_TO_DATE() Example

This converts the string '21-11-2025' into a proper DATE value: 2025-11-21.

2. DATE_FORMAT(): Convert Date → String
  1. 1

    Formats a date value into a custom string.

  2. 2

    Useful for displaying dates in user-friendly formats.

  3. 3

    Requires format specifiers similar to STR_TO_DATE().

DATE_FORMAT() Example

This outputs: '21/11/2025'.

3. Key Differences Between STR_TO_DATE() and DATE_FORMAT()
  1. 1

    STR_TO_DATE() converts a string into a date value.

  2. 2

    DATE_FORMAT() converts a date into a formatted string.

  3. 3

    STR_TO_DATE() is used when reading/parsing dates; DATE_FORMAT() is used when displaying dates.

  4. 4

    Both rely on the same set of format specifiers.

In summary: Use STR_TO_DATE() to interpret a date string into a real MySQL date, and DATE_FORMAT() to output a stored date in a human-readable format.

Difficulty: 5/10
Topics: date parsing, date formatting, SQL functions

Scenario Questions

0-2 years experience
  1. 1

    We have a CSV import where dates appear as '31/12/2022'. Write the INSERT statement that converts that string into a DATE column using MySQL functions.

  2. 2

    If you run SELECT DATE_FORMAT('2022-07-15', '%b %d, %Y'), what string is returned and why?

2-5 years experience
  1. 1

    Our reporting feature expects dates in 'YYYYMMDD' format, but the source table stores them as VARCHAR in 'MM-DD-YYYY'. How would you write a SELECT that returns the dates correctly converted and formatted?

  2. 2

    During a data migration some rows contain malformed date strings, causing STR_TO_DATE to return NULL. How would you identify those rows and provide a fallback value in a single query?

5-8 years experience
  1. 1

    We need a daily summary table that aggregates events by date, yet event timestamps are stored as strings in several legacy formats across tables. Describe a robust conversion pipeline using STR_TO_DATE and DATE_FORMAT, addressing performance and maintainability.

  2. 2

    Our application serves millions of users and frequently formats dates for UI. Discuss the trade‑offs of doing date formatting in MySQL with DATE_FORMAT versus handling it in the application layer.

8+ years experience
  1. 1

    Our organization is migrating from MySQL to a polyglot data platform. How would you refactor all existing STR_TO_DATE and DATE_FORMAT usage to ensure consistency, backward compatibility, and minimal disruption across services?

  2. 2

    Multiple microservices store dates in different string representations due to historic reasons. Propose an architectural strategy to standardize date handling across the ecosystem, including migration plan, testing, and governance.

Follow-up Questions

  • What edge cases might cause STR_TO_DATE to return NULL?
  • How would you test that your formatting works for all locales?
  • Can you estimate the performance impact of formatting millions of rows in a query?